interface Timer {
    start: () => void;
    reset: () => void;
    stop: () => void;
    onTimeout: (callback: () => void) => void;
}

function createTimer(timeout: number): Timer {
    let timerId: NodeJS.Timeout;
    let startTime: number;
    let onTimeoutCallback: () => void;

    function start() {
        startTime = Date.now();
        timerId = setTimeout(() => {
            console.log('Timeout');
            if (onTimeoutCallback) {
                onTimeoutCallback();
            }
        }, timeout);
    }

    function reset() {
        clearTimeout(timerId);
        start();
    }

    function stop() {
        clearTimeout(timerId);
    }

    function onTimeout(callback: () => void): void {
        onTimeoutCallback = callback;
    }

    return {
        start,
        reset,
        stop,
        onTimeout
    };
}
//   // 使用示例
//   const timer: Timer = createTimer(5000);
//   timer.start(); // 开始计时器
//   setTimeout(() => {
//     timer.reset(); // 重置计时器
//   }, 2000);


export default createTimer